package com.xm.xmapp;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.xm.xmapp.adapter.ViewPagerAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.MyViewPager;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.PermissionsChecker;

public class GuideActivity extends BaseActivity {

	// // 所需的全部权限
	// static final String[] PERMISSIONS = new String[] {
	// android.Manifest.permission.MODIFY_AUDIO_SETTINGS,
	// android.Manifest.permission.CALL_PHONE };
	private GestureDetector gestureDetector;
	PermissionsChecker mPermissionsChecker;
	// private static final int REQUEST_CODE = 0; // 请求码
	private ViewPager viewPager;
	private View pager0;
	private View pager1;
	private View pager2;
	private View pager3;
	private ImageView[] img;
	int currentItem;
	/**
	 * 用来存储pager的
	 */
	private List<View> mViews;

	private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			float x = e2.getX() - e1.getX();
			if (x < 0 && currentItem == (img.length - 1)) {
				Helper_SharedPreferences.set_bool_sp(CodeConstant.YseOrNo, true);
				intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
				startActivity(intent);
				finish();
				return true;
			} else {
				return false;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		isUsePad = false;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		super.onCreate(savedInstanceState);
		mPermissionsChecker = new PermissionsChecker(this);
		if (Helper_SharedPreferences.get_bool_sp(CodeConstant.YseOrNo)) {
			Helper_SharedPreferences.set_bool_sp(CodeConstant.YseOrNo, true);
			intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
			startActivity(intent);
			finish();
		} else {
			gestureDetector = new GestureDetector(this, onGestureListener);
			setContentView(R.layout.activity_guide);
		}

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if (gestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
		return super.dispatchTouchEvent(event);
	}

	@Override
	public void initView() {

		viewPager = (ViewPager) findViewById(R.id.guide_activity_viewpager);
		img = new ImageView[4];
		img[0] = (ImageView) findViewById(R.id.img0);
		img[1] = (ImageView) findViewById(R.id.img1);
		img[2] = (ImageView) findViewById(R.id.img2);
		img[3] = (ImageView) findViewById(R.id.img3);
		pager0 = LayoutInflater.from(this).inflate(R.layout.view_guide_page0, null);
		pager1 = LayoutInflater.from(this).inflate(R.layout.view_guide_page1, null);
		pager2 = LayoutInflater.from(this).inflate(R.layout.view_guide_page2, null);
		pager3 = LayoutInflater.from(this).inflate(R.layout.view_guide_page3, null);
	}

	@Override
	public void initData() {

		mViews = new ArrayList<View>();
		mViews.add(pager0);
		mViews.add(pager1);
		mViews.add(pager2);
		mViews.add(pager3);
	}

	@Override
	public void initEvent() {

		viewPager.setAdapter(new ViewPagerAdapter(mViews));
		viewPager.addOnPageChangeListener(new MyViewPager.MyOnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				currentItem = arg0;
				for (int i = 0; i < img.length; i++) {
					if (arg0 == i) {
						img[i].setImageResource(R.mipmap.xyd);
					} else {
						img[i].setImageResource(R.mipmap.yd);
					}
				}
			}
		});
	}

}
